package com.example.administrator.maizuofilm.httputils;

import com.alibaba.fastjson.JSON;
import com.example.administrator.maizuofilm.moviedetails.bean.Comment;
import com.example.administrator.maizuofilm.moviedetails.bean.Details;
import com.example.administrator.maizuofilm.moviedetails.bean.DetailsComment;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;

import httputils.UrlConfig;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by hansen on 2016/7/12.
 */
public class HttpUtils {
private  IRetrofitHttp http;

private static  HttpUtils httputils;
    public synchronized static HttpUtils newInstance() {
        if (httputils == null) {
            httputils = new HttpUtils();
        }
        return httputils;
    }


    private HttpUtils(){
        Retrofit retrofit=new Retrofit.Builder().baseUrl(UrlConfig.MAIZUOFILM)
                .build();
        http=retrofit.create(IRetrofitHttp.class);

    }
public void  getCommend(String path){
Call<ResponseBody> call=http.getComment(path);
    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            try {
                Comment comment=JSON.parseObject(response.body().string(),Comment.class);


                EventBus.getDefault().post(comment);
            } catch (IOException e) {
                e.printStackTrace();
                EventBus.getDefault().post(null);
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
           // EventBus.getDefault().post(null);
        }
    });

}

public  void getMovieDetailsInfo(String path){
    Call<ResponseBody> call=http.getMovieDetails(path);
    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            try {
                Details details= JSON.parseObject(response.body().string(),Details.class);
                EventBus.getDefault().post(details);
            } catch (IOException e) {
                e.printStackTrace();
                EventBus.getDefault().post(null);
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
   //         EventBus.getDefault().post(null);
        }
    });

}
    public void  getDetailsComment(String path){
        Call<ResponseBody> call=http.getDetailsComment(path);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    DetailsComment comm=JSON.parseObject(response.body().string(),DetailsComment.class);
                    EventBus.getDefault().post(comm);

                } catch (IOException e) {

                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }

}
